import scrapy
from weibovideo.items import WeibovideoItem


class WeiboSpider(scrapy.Spider):
    name = 'weibo'
    allowed_domains = ['weibo.com', 'f.video.weibocdn.com']
    start_urls = ['https://weibo.com/tv/api/component?page=/tv/channel/4379156922732905']
    # 表单数据
    cursor = 1
    data = 'data={"Component_Channel_Hot":{"next_cursor":' + f"{cursor}" + ',"cid":"4379156922732905"}}'

    #  重写开始的请求的方法为POST
    def start_requests(self):
        for url in self.start_urls:
            yield scrapy.Request(url=url, callback=self.parse, method='POST', body=self.data)

    def parse(self, response):
        #  获取json数据
        data = response.json()
        print(data)
        #  判断是否有返回ｕ数据
        if data['code'] == "100000":
            #  获取所有的vlog列表
            vlog_list = data['data']['Component_Channel_Hot']['list']
            #  遍历所有vlog
            for vlog in vlog_list:
                #  视频的oid
                oid = vlog['oid']
                #  发送POST请求 , 获取json数据
                url = f"https://weibo.com/tv/api/component?page=/tv/show/{oid}"
                #  要提交的表单数据
                data = 'data={"Component_Play_Playinfo":{"oid":"' + f'{oid}' + '"}}'
                yield scrapy.Request(url=url, method='POST', body=data,
                                     callback=self.parseJson)
        #  下一页的热门视频
        self.cursor += 1
        formdata = 'data={"Component_Channel_Hot":{"next_cursor":' + f"{self.cursor}" + ',"cid":"4379156922732905"}}'
        yield scrapy.Request(url=self.start_urls[0], method='POST', body=formdata, callback=self.parse)
        # self.start_requests()

    #  获取视频的url
    def parseJson(self, response):
        data = response.json()
        items = WeibovideoItem()
        if data['code'] == "100000":
            #  标题
            try:
                title = data['data']['Component_Play_Playinfo']['title']
                vlog = data['data']['Component_Play_Playinfo']['urls']['高清 1080P']
            except KeyError as k:
                print(k)
            else:
                items['title'] = title
                url = "https:" + vlog
                items['url'] = url
                yield items
